package cn.iocoder.yudao.module.team.controller.admin.scenicspot;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.team.controller.admin.scenicspot.vo.*;
import cn.iocoder.yudao.module.team.dal.dataobject.scenicspot.ScenicspotDO;
import cn.iocoder.yudao.module.team.service.scenicspot.ScenicspotService;

@Tag(name = "管理后台 - 景点资料")
@RestController
@RequestMapping("/team/scenicspot")
@Validated
public class ScenicspotController {

    @Resource
    private ScenicspotService scenicspotService;

    @PostMapping("/create")
    @Operation(summary = "创建景点资料")
    @PreAuthorize("@ss.hasPermission('team:scenicspot:create')")
    public CommonResult<Integer> createScenicspot(@Valid @RequestBody ScenicspotSaveReqVO createReqVO) {
        return success(scenicspotService.createScenicspot(createReqVO));
    }


    @PutMapping("/update")
    @Operation(summary = "更新景点资料")
    @PreAuthorize("@ss.hasPermission('team:scenicspot:update')")
    public CommonResult<Boolean> updateScenicspot(@Valid @RequestBody ScenicspotSaveReqVO updateReqVO) {
        scenicspotService.updateScenicspot(updateReqVO);
        return success(true);
    }
    @PutMapping("/updateCreateBatch")
    @Operation(summary = "更新景点资料")
    @PreAuthorize("@ss.hasPermission('team:scenicspot:update')")
    public CommonResult<Boolean> updateCreateBatchScenicspot(@Valid @RequestBody List<ScenicspotDO> updateReqVO) {
        scenicspotService.updateCreateBatchScenicspot(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除景点资料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('team:scenicspot:delete')")
    public CommonResult<Boolean> deleteScenicspot(@RequestParam("id") Integer id) {
        scenicspotService.deleteScenicspot(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除景点资料")
                @PreAuthorize("@ss.hasPermission('team:scenicspot:delete')")
    public CommonResult<Boolean> deleteScenicspotList(@RequestParam("ids") List<Integer> ids) {
        scenicspotService.deleteScenicspotListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得景点资料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('team:scenicspot:query')")
    public CommonResult<ScenicspotRespVO> getScenicspot(@RequestParam("id") Integer id) {
        ScenicspotDO scenicspot = scenicspotService.getScenicspot(id);
        return success(BeanUtils.toBean(scenicspot, ScenicspotRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得景点资料分页")
    @PreAuthorize("@ss.hasPermission('team:scenicspot:query')")
    public CommonResult<PageResult<ScenicspotRespVO>> getScenicspotPage(@Valid ScenicspotPageReqVO pageReqVO) {
        PageResult<ScenicspotDO> pageResult = scenicspotService.getScenicspotPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ScenicspotRespVO.class));
    }
    @GetMapping("/list")
    @Operation(summary = "获得景点资料")
    @PreAuthorize("@ss.hasPermission('team:scenicspot:query')")
    public  CommonResult<List<ScenicspotRespVO>> getScenicspotlist(@Valid ScenicspotPageReqVO pageReqVO) {
        List<ScenicspotDO> list = scenicspotService.getScenicspotPage(pageReqVO).getList();
        return success(BeanUtils.toBean(list, ScenicspotRespVO.class));
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出景点资料 Excel")
    @PreAuthorize("@ss.hasPermission('team:scenicspot:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportScenicspotExcel(@Valid ScenicspotPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ScenicspotDO> list = scenicspotService.getScenicspotPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "景点资料.xls", "数据", ScenicspotRespVO.class,
                        BeanUtils.toBean(list, ScenicspotRespVO.class));
    }

}